package utils;
import android.util.Log;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by Administrator on 2015/4/22.
 */
public class HttpUtil {

    public static void getJson(final String path, final CallBack callBack){
        new Thread(){
            @Override
            public void run() {
                super.run();
                HttpClient client=new DefaultHttpClient();
                    HttpGet get=new HttpGet(path);
                    try {
                        HttpResponse res=client.execute(get);
                        if (res.getStatusLine().getStatusCode()==200){
                            String jsonString= EntityUtils.toString(res.getEntity());
                             if (jsonString!=null)
                            callBack.getResult(jsonString);
                        }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    public static void getJsonString(String path, final CallBack callBack){
        HttpUtils utils=new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> Info) {
                if (Info!=null)
                callBack.getResult( Info.result);

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.i("123","onFailure---->"+s);
            }
        });

    }

    public interface CallBack{
        public void getResult(String jsonString);
    }
}
